<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Home extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('login')) {
            redirect('login');
        } else {
            $this->load->model('mtransaksi_jual');
        }
    }

    public function index() {
        $data['title'] = 'Transaksi';
        $data['page'] = 'vhome';
        $transaksi = $this->mtransaksi_jual->get_chart_per_tanggal(date('m'),date('Y'));
        $bln = "";
        $total = "";
        foreach ($transaksi->result() as $row) {
            $bln = $bln . "','" . $row->tglbln;
            $total = $total . ',' . $row->total;
        }
        $bln = substr($bln, 2) . "'";
        $total = substr($total, 1);
        $data['name'] = 'All Retail';
        $data['bulan'] = $bln;
        $data['tahun'] = date('Y');
        $data['total'] = $total;
        $this->load->view('index', $data);
    }

    function get_per_hari() {
        $bulan = str_pad($this->input->post('bulan'), 2, '0', STR_PAD_LEFT);
        $tahun = $this->input->post('tahun');
        $menu = $this->mtransaksi_jual->get_chart_per_tanggal($bulan, $tahun);

        if ($menu == null) {
            echo 'no data';
        }

        $arr_thbl = array();
        $arr_total = array();

        $i = 0;
        foreach ($menu->result() as $row) {
            $arr_thbl[$i] = "" . $row->tglbln . "";
            $arr_total[$i] = round($row->total);
            $i++;
        }

        $json = array(
            'tahun' => date('Y'),
            'name' => "All Retail",
            'bulan' => $arr_thbl,
            'total' => $arr_total
        );
        echo json_encode($json);
    }
    
    function get_per_bulan() {
        $bulan = str_pad($this->input->post('bulan'), 2, '0', STR_PAD_LEFT);
        $tahun = $this->input->post('tahun');
        $menu = $this->mtransaksi_jual->get_chart_per_bulan($bulan, $tahun);

        if ($menu == null) {
            echo 'no data';
        }

        $arr_thbl = array();
        $arr_total = array();

        $i = 0;
        foreach ($menu->result() as $row) {
            $arr_thbl[$i] = "" . $row->bl. "";
            $arr_total[$i] = round($row->total);
            $i++;
        }

        $json = array(
            'tahun' => date('Y'),
            'name' => "All Retail",
            'bulan' => $arr_thbl,
            'total' => $arr_total
        );
        echo json_encode($json);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */